/**
 *     This file is part of cesm-ws.
 *
 *     cesm-ws, Web Service for Community Earth System Model
 *
 *     Copyright (c) 2010, Purdue University and/or affilicates or thrid-party contributors as
 *     indicated by the @author tags or express copyright attribution statements
 *     applied by the authors. All third-party contributions are distributed under
 *     license by Purdue University.
 *
 *     cesm-ws is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this distribution.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.purdue.rcac.cesm.facade.bean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "var")
@XmlAccessorType(XmlAccessType.FIELD)
public class KeyValueEntry {

	@XmlAttribute(name = "key", namespace = "")
	private String key;

	@XmlAttribute(name = "value", namespace = "")
	private String value;

	public KeyValueEntry(String key, String value) {
		super();
		this.key = key;
		this.value = value;
	}

	public KeyValueEntry() {
		super();
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String toString(String delimiter) {
		return String.format("%s%s%s", key, delimiter, value);
	}

	public static KeyValueEntry fromString(String str) throws Exception {
		String[] parts = str.split("=");
		if (parts.length != 2)
			throw new Exception("can not parse string '" + str
					+ "' into KeyValueEntry");
		return new KeyValueEntry(parts[0], parts[1]);
	}
}
